package org.arain.baseii.system.quick;

import java.util.Date;
import java.util.List;

import org.arain.baseii.exception.UseException;
import org.arain.baseii.mapper.SysUserQuickMapper;
import org.arain.baseii.pojo.SysResource;
import org.arain.baseii.pojo.SysUserQuick;
import org.arain.baseii.pojo.SysUserQuickExample;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.ArainResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
/**
 * 
* <p>Title:QuickServiceImpl </p>
* @author Arain
* @date2017年9月7日
 */
@Service
@Transactional
public class QuickServiceImpl implements QuickService{
	@Autowired
	private SysUserQuickMapper sysUserQuickMapper;
	@Override
	public ArainResult save_01(QuickDto dto) {
		SysUserQuickExample example = new SysUserQuickExample();
		example.createCriteria().andUserIdEqualTo(AppUtils.getUserId()).andResourceIdEqualTo(Long.valueOf(dto.getResourceId()));
		List<SysUserQuick> list = sysUserQuickMapper.selectByExample(example);
		if(list!=null && list.size()>0) {
			throw new UseException("快捷菜单已存在");
		}
		SysUserQuick quick = new SysUserQuick();
		AppUtils.copyProperties(quick, dto);
		quick.setDate(new Date());
		quick.setUserId(AppUtils.getUserId());
		sysUserQuickMapper.insert(quick);
		return ArainResult.ok();
	}
	@Override
	public String load_01(QuickDto dto) {
		JSONArray array = new JSONArray();
		List<SysResource> list = sysUserQuickMapper.selectByUserId(AppUtils.getUserId());
		for (SysResource resource : list) {
			JSONObject object = new JSONObject();
			object.put("id", resource.getId());
			object.put("text", "<i class=\""+resource.getIcon()+" fa-2x\"></i><p>"+resource.getName()+"</p>");
			object.put("url", resource.getTarget());
			object.put("iconCls", resource.getIcon());
			array.add(object);
		}
		JSONObject open = new JSONObject();
		open.put("id",-100 );
		open.put("text", "<a class=\"left-control-switch\"><i class=\"fa fa-chevron-right fa-2x\" style=\"color:red\"></i><p style=\"color:red\">打开</p></a></li>");
		array.add(open);
		return array.toString();
	}
	@Override
	public ArainResult delete_01(QuickDto dto) {
		SysUserQuickExample example = new SysUserQuickExample();
		example.createCriteria().andUserIdEqualTo(AppUtils.getUserId()).andResourceIdEqualTo(Long.valueOf(dto.getResourceId()));
		List<SysUserQuick> list = sysUserQuickMapper.selectByExample(example);
		if(list == null || list.size() <= 0) {
			throw new UseException("此快捷菜单不存在");
		}
		sysUserQuickMapper.deleteByExample(example);
		return ArainResult.ok();
	}

}
